package com.xmim.fulcrum.handler.impl;

import com.xmim.core.constants.MessageCode;
import com.xmim.core.entity.Message;
import com.xmim.core.entity.Notification;
import com.xmim.core.session.user.UserSession;
import com.xmim.fulcrum.FulcrumDiscover;
import com.xmim.fulcrum.handler.NotificationMessageHandler;
import com.xmim.fulcrum.node.FulcrumUserNode;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NotificationHandler extends NotificationMessageHandler {


    public NotificationHandler(FulcrumDiscover fulcrumDiscover) {
        super(fulcrumDiscover);
    }

    @Override
    public void handler0(UserSession session, Message<Notification> message) {
        //处理消息的内容
        Notification notification = message.getBody();
        //节点的链接成功
        if (MessageCode.NTF_CONNECT_FINISHED == notification.getType()) {
            log.debug("收到分布式客户端连接成功通知, node={}", notification);
            FulcrumUserNode fulcrumNode = (FulcrumUserNode) notification.getData();
            session.addAttribute("CHANNEL_NAME", fulcrumNode);
        }
        //上线的通知
        if (MessageCode.NTF_USER_ONLINE == notification.getType()) {
            log.debug("收到分布式客户端用户上线通知, node={}", notification);
            FulcrumUserNode fulcrumNode = (FulcrumUserNode) notification.getData();
            fulcrumDiscover.getUserCache().addNode(fulcrumNode);
        }
        //下线的通知
        if (MessageCode.NTF_USER_OFFLINE == notification.getType()) {
            log.debug("收到分布式客户端用户下线通知, node={}", notification);
            FulcrumUserNode fulcrumNode = (FulcrumUserNode) notification.getData();
            fulcrumDiscover.getUserCache().removeNode(fulcrumNode.getSessionId());
        }
        //发送测试消息
        if (MessageCode.NTF_TEST_MSG == notification.getType()) {
            log.info("收到分布式客户端测试消息, node={}", notification);
        }
    }
}
